package com.project.rfidpage.controller;


import com.github.pagehelper.PageInfo;
import com.project.rfidpage.aop.TagLog;
import com.project.rfidpage.dto.IcManuParam;
import com.project.rfidpage.dto.RidLivParam;
import com.project.rfidpage.dto.SearchByRqParam;
import com.project.rfidpage.entity.IcManu;
import com.project.rfidpage.service.IcManuService;
import com.project.rfidpage.util.RestResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/rfidpage/icmanu/")
public class IcManuController extends BaseController {

    @Resource
    IcManuService icManuService;

    /**
     * 列表查询
     */
    @PostMapping("page")
    @TagLog(operation = 0, content = "查询电子标签列表")
    public RestResult page(@RequestBody IcManuParam param) {
        this.getPage(true, param);
        return RestResult.ok(new PageInfo<>(icManuService.getListForPage(param)));
    }

    /**
     * 导出列表
     */
    @PostMapping("output")
    @TagLog(operation = 5, content = "导出电子标签列表")
    public RestResult output(@RequestBody IcManuParam param) {
        this.getPage(false, param);
        return RestResult.ok(icManuService.getListForPage(param));
    }

    /**
     * 运转情况列表查询
     */
    @PostMapping("liv/page")
    @TagLog(operation = 0, content = "查询电子标签运转情况列表")
    public RestResult livPage(@RequestBody RidLivParam param) {
        this.getPage(true, param);
        return RestResult.ok(new PageInfo<>(icManuService.getLivListForPage(param)));
    }

    /**
     * 运转情况导出列表
     */
    @PostMapping("liv/output")
    @TagLog(operation = 5, content = "导出电子标签运转情况列表")
    public RestResult livOutput(@RequestBody RidLivParam param) {
        this.getPage(false, param);
        return RestResult.ok(icManuService.getLivListForPage(param));
    }

    /**
     * 检查电子标签卡号
     */
    @PostMapping("check")
    @TagLog(operation = 0, content = "检查电子标签卡号")
    public RestResult check(@RequestBody IcManuParam param) {
        List<IcManu> list = icManuService.getByIcNo(param.getIcno());
        if (list.size() == 0) {
            return RestResult.ok("电子标签卡号可用");
        } else {
            return RestResult.error("电子标签卡号已经存在");
        }
    }

    /**
     * 新增电子标签
     */
    @PutMapping("add")
    @TagLog(operation = 2, content = "新增电子标签")
    public RestResult add(@RequestHeader(value = "user") String user, @RequestBody IcManu param) {
        List<IcManu> list = icManuService.getByIcNo(param.getIcno());
        if (list.size() == 0) {
            try {
                param.setTid_vender_regnum(param.getIcno().substring(0, 2));
                param.setInputdt(new Date());
                icManuService.insert(param);
                return RestResult.ok();
            } catch (Exception e) {
                return RestResult.error(e.getMessage());
            }
        } else {
            return RestResult.error("电子标签卡号已经存在");
        }
    }

    /**
     * 更新电子标签
     */
    @PutMapping("update")
    @TagLog(operation = 3, content = "更新电子标签")
    public RestResult update(@RequestHeader(value = "user") String user, @RequestBody IcManu param) {
        List<IcManu> list = icManuService.getByIcNo(param.getIcno());
        if (list.size() == 0) {
            return RestResult.error("该条数据不存在");
        } else {
            try {
                param.setTid_vender_regnum(param.getIcno().substring(0, 2));
                param.setInputdt(new Date());
                icManuService.update(param, list.get(0).getIcno());
                return RestResult.ok();
            } catch (Exception e) {
                return RestResult.error(e.getMessage());
            }
        }
    }

    /**
     * 电子标签营运质量日报列表查询
     */
    @PostMapping("daily/run/page")
    @TagLog(operation = 0, content = "查询电子标签营运质量日报列表")
    public RestResult dailyRunPage(@RequestBody SearchByRqParam param) {
        this.getPage(true, param);
        return RestResult.ok(new PageInfo<>(icManuService.getDailyListForPage(param)));
    }

    /**
     * 基站营运质量日报导出列表
     */
    @PostMapping("daily/run/output")
    @TagLog(operation = 5, content = "导出电子标签营运质量日报列表")
    public RestResult dailyRunOutput(@RequestBody SearchByRqParam param) {
        this.getPage(false, param);
        return RestResult.ok(icManuService.getDailyListForPage(param));
    }
}
